//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl

package cn.agiledata.bank.transaction.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.service.PermissionService;
import cn.agiledata.bank.common.service.SetUpUses;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ConvertUtil;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.service.GetTransaction;
import cn.agiledata.bank.transaction.service.GetTransactionImpl;
import cn.agiledata.bank.transaction.service.TransactionFactory;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.logic.IPBMgrFactory;

/**
 * MyEclipse Struts Creation date: 04-19-2006
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 * @struts.action-forward name="subAccountOutSuccess"
 *                        path="subAccountOutSuccess"
 */
public class SubAccountOutAction extends IPBBaseAction {
	// --------------------------------------------------------- Instance
	// Variables

	// --------------------------------------------------------- Methods

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward preProcess(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
		throws Exception{
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		PermissionService pif = PermissionService.getPermissionService();
		if (!pif.hasPermission(request,Constant.PERMIS_TYPE_TRANSFER)){
			return mapping.findForward("permission");
		}
		ActionForward af = new ActionForward();
		String Action= request.getParameter("action");
		DynaActionForm fill = (DynaActionForm)form;

			AccountTemplateInter at = 
				(AccountTemplateInter)IPBMgrFactory.getMgr("accountTemplate");
			
		
		Account acco  = at.getAccount(String.valueOf(accountId));
		//add by cjh : aim at retriving the valid subaccount
		
		SubAccount defaultSA = acco.getDefaultSubAccount();
		if(!pif.isCorpOperator(request)){
			Set set = acco.getSubAccounts();
			set.remove(acco.getDefaultSubAccount());
			Iterator it = set.iterator();
			List subAccountSet = new ArrayList();
			subAccountSet.add(acco.getDefaultSubAccount());
			
			for(;it.hasNext();) {
				SubAccount subaccount = (SubAccount)it.next();
				if("Y".equalsIgnoreCase(subaccount.getValidFlag())) {
					subAccountSet.add(subaccount);
				}
			}
		//add by cjh above
			request.setAttribute("subAccount",subAccountSet);
		}else{
			request.setAttribute("subAccount", 
					pif.getSubAccounts(request, Constant.PERMIS_TYPE_TRANSFER, defaultSA));
		}

		if (fill.get("ifFinished").equals("true")){
			WeatherUtil.cleanForm(fill);
			//fill.setIfFinished(true);
			return mapping.findForward("subAccountOutPre");
		}
		
		if (Action!= null && Action.equals("main")){
			WeatherUtil.cleanForm(fill);
			return mapping.findForward("subAccountOutPre");
		}
		if (fill.get("whichPage").equals("1")){
			af = mapping.findForward("subAccountOutPre");
		}		
		if (fill.get("whichPage").equals("2")){
			Transaction tran = new Transaction();
			encapTransaction(tran,fill,acco,at);
			
			//保存信息
			GetTransaction getTran = new GetTransactionImpl();
			TransactionFactory tt = 
				getTran.getTransaction(Constant.ACTION_TYPE_H);
			request.setAttribute("tran",tran);
			
			try {
				tt.saveTransaction(tran);
				SetUpUses ss = new SetUpUses();
				String url = WeatherUtil.getRootFromSession(request);
				ss.sendSetup(tran, tran.getCredit(), Constant.ACTION_TYPE_H,tran.getDebitFee().toString(),ConvertUtil.parseDec(new Double(tran.getAmount()
						.doubleValue()- tran.getDebitFee().doubleValue()), "0.00"),url,null);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				fill.set("ifFinished","true");
			}
			af = mapping.findForward("subAccountOutSuccess");
		}
		
		return af;
	}
	private void encapTransaction(
			Transaction tran,
			DynaActionForm obj,
			Account acco,
			AccountTemplateInter at)throws Exception{
		ActionTypeTemplate att = 
			(ActionTypeTemplate)IPBMgrFactory.getMgr("actionTypeTemplate");
		
		tran.setDebit(at.getSubAccount(obj.getString("accountFrom")));
		tran.setCredit(at.getSubAccount(obj.getString("accountTo")));
		
		//封装插入日期和修改日期
		WeatherUtil.fillCreateAndUpdateInfo(tran,acco.getId().toString());
		//
		tran.setBeginDate(new Date());
		UniqueIdentityUtil uiu = new UniqueIdentityUtil();
		tran.setTransId(uiu.getTransactionSequence(Constant.ACCOUNT_TYPE_BUYER,Constant.MODEL_TYPE_BANKDEPOSIT));
		
		tran.setActionType(att.getActionTypeById(Long.parseLong(Constant.ACTION_TYPE_H)));
		//交易金额
		tran.setAmount(new Double(obj.getString("count")));
		
		tran.setTradeType(Constant.TRADE_TYPE_NOASSURE);
		tran.setTransStatus(Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
		
		
	}
}
